package inf.coach.mobile;

import java.util.Locale;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class Ampel extends Activity {
	
    public static long userid = 0;
    public static long osmid= 0;
    public CheckBox audio;
    public CheckBox zeichen;
    public CheckBox direction;
    public CheckBox vibration;
    public Button send;
    
	public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ampelanno);
        setTitle("Funktion Seite, Ampel Informationen");
        
       	Bundle bundle = this.getIntent().getExtras();  
		userid = bundle.getLong("userid");
		osmid= bundle.getLong("osmid");		
		
		audio=(CheckBox)findViewById(R.id.checkBox_ampel_audio);
		zeichen=(CheckBox)findViewById(R.id.checkBox_ampel_zeichen);
		direction=(CheckBox)findViewById(R.id.checkBox_ampel_direction);
		vibration=(CheckBox)findViewById(R.id.checkBox_ampel_vibration);
		send=(Button) findViewById(R.id.button_ampel_senden);
		
		final boolean flag =bundle.getBoolean("flag");
		
		
		if(flag)
		{

			audio.setChecked(bundle.getBoolean("trafficlights_audio"));
			audio.setClickable(false);
			zeichen.setChecked(bundle.getBoolean("trafficlights_insideisland"));
			zeichen.setClickable(false);
			direction.setChecked(bundle.getBoolean("trafficlights_directionsymbol"));
			direction.setClickable(false);
			vibration.setChecked(bundle.getBoolean("trafficlights_vibration"));
			vibration.setClickable(false);
			
			
			
		}    
     
        send.setOnClickListener(new Button.OnClickListener(){
        	
        	public void onClick(View arg0){
        		if(flag && send.getText().equals("Bearbeiten"))
        		{
        			audio.setClickable(true);
        			zeichen.setClickable(true);
        			direction.setClickable(true);
        			vibration.setClickable(true);
        			send.setText("Absenden");
        			send.setContentDescription("Button, absenden ");
        		}
        		else if(flag && send.getText().equals("Absenden"))
        		{
            		if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
    				{
    									
    				String text = webservice.updateAmpelAnno(userid, osmid, audio.isChecked(), zeichen.isChecked(), direction.isChecked(), vibration.isChecked());
    				System.out.println(text);
    				if (!text.equals(null) && text.equals("successful")) {
    					dialog();

    				}
    				if (text.equals(null) || text.equals("failed")) {
    					message.show(Ampel.this,"Fehlgeschlagen", "Bitte überprüfen Internetverbindung!");
    				}
    				}
            			else{
        					message.show(Ampel.this,"Fehler", "Keine Internetverbindung !");
        				}
        		}
        		else
        		{
        		if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
				{
									
				String text = webservice.createAmpelAnno(userid, osmid, audio.isChecked(), zeichen.isChecked(), direction.isChecked(), vibration.isChecked());
				System.out.println(text);
				if (!text.equals(null) && text.equals("successful")) {
					dialog();

				}
				if (text.equals(null) || text.equals("failed")) {
					message.show(Ampel.this,"Fehlgeschlagen", "Bitte überprüfen Internetverbindung!");
				}
				}
        			else{
    					message.show(Ampel.this,"Fehler", "Keine Internetverbindung !");
    				}
        		}
        	}
        	
        });
      
       
     
	}
	
	protected void dialog() { 
        AlertDialog.Builder builder = new Builder(Ampel.this); 
        builder.setMessage("Automatisch zur letzten Seite zurück!"); 
        builder.setTitle("Erfolg"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        Ampel.this.finish();
                    }

                }); 
        
        builder.create().show(); 
    } 
    protected void dialoggoback() { 
        AlertDialog.Builder builder = new Builder(Ampel.this); 
        builder.setMessage("Möchten Sie zur letzten Seite zurück springen?"); 
        builder.setTitle("Information"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        Ampel.this.finish(); 
                    }

                }); 
        builder.setNegativeButton("abbrechen", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                    } 
                }); 
        builder.create().show(); 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
            dialoggoback(); 
            return false; 
        } 
        return false; 
    } 
	
}






